package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ISalaryFeedbackDAO;
import org.jsola.hr.dto.SalaryFeedbackAddDTO;
import org.jsola.hr.dto.SalaryFeedbackUpdateDTO;
import org.jsola.hr.entity.SalaryFeedbackDO;
import org.jsola.hr.query.SalaryFeedbackQuery;
import org.jsola.hr.service.ISalaryFeedbackService;
import org.jsola.hr.vo.SalaryFeedbackListVO;
import org.jsola.hr.vo.SalaryFeedbackVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 工资条反馈记录
 *
 * @author zhr
 */
@Service("hrSalaryFeedbackServiceImpl")
public class SalaryFeedbackServiceImpl implements ISalaryFeedbackService {

    @Autowired
    private ISalaryFeedbackDAO salaryFeedbackDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public SalaryFeedbackVO save(SalaryFeedbackAddDTO salaryFeedbackAddDTO, TokenUser tokenUser) {
        // 转DO
        SalaryFeedbackDO salaryFeedbackDO = salaryFeedbackAddDTO.to(SalaryFeedbackDO.class);
        // 保存
        salaryFeedbackDO = save(salaryFeedbackDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return salaryFeedbackDO.to(SalaryFeedbackVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(SalaryFeedbackUpdateDTO salaryFeedbackUpdateDTO, TokenUser tokenUser) {
        //转DO
        SalaryFeedbackDO salaryFeedbackDO = salaryFeedbackUpdateDTO.to(SalaryFeedbackDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(salaryFeedbackDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... salaryFeedbackIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) salaryFeedbackIds);
    }


    @Override
    public SalaryFeedbackVO selectById(String salaryFeedbackId, String siteId) {
        SalaryFeedbackDO salaryFeedbackDO = selectDOById(salaryFeedbackId, siteId);
        if (salaryFeedbackDO == null) {
            return null;
        }
        return salaryFeedbackDO.to(SalaryFeedbackVO.class);
    }

    @Override
    public List<SalaryFeedbackListVO> select(SalaryFeedbackQuery salaryFeedbackQuery, String siteId) {
        List<SalaryFeedbackDO> salaryFeedbackDOList = selectDO(salaryFeedbackQuery, siteId);
        if(CollectionUtils.isEmpty(salaryFeedbackDOList)) {
            return salaryFeedbackDOList == null ? null : new ArrayList<>();
        }
        return salaryFeedbackDOList.stream()
                .map(salaryFeedbackDO -> salaryFeedbackDO.to(SalaryFeedbackListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(SalaryFeedbackQuery salaryFeedbackQuery, String siteId) {
        Example example = buildExample(salaryFeedbackQuery,siteId);
        return salaryFeedbackDAO.selectCountByExample(example);
    }

    @Override
    public Page<SalaryFeedbackListVO> selectPage(SalaryFeedbackQuery salaryFeedbackQuery, String siteId) {
        Example example = buildExample(salaryFeedbackQuery,siteId);
        Page<SalaryFeedbackDO> page = salaryFeedbackDAO.selectPageByExample(example,
                salaryFeedbackQuery.getPageNo(),
                salaryFeedbackQuery.getPageSize());

        return page.to(SalaryFeedbackListVO.class);
    }

    @Override
    public SalaryFeedbackDO selectDOById(String salaryFeedbackId, String siteId) {
        return listById(salaryFeedbackId, siteId);
    }

    @Override
    public List<SalaryFeedbackDO> selectDO(SalaryFeedbackQuery salaryFeedbackQuery, String siteId) {
        Example example = buildExample(salaryFeedbackQuery,siteId);
        return salaryFeedbackDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param salaryFeedbackQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(SalaryFeedbackQuery salaryFeedbackQuery, String siteId) {
        Example example = new Example(SalaryFeedbackDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (salaryFeedbackQuery.getBillId() != null) {
            example.and().andEqualTo("billId", salaryFeedbackQuery.getBillId());
        }
        if (salaryFeedbackQuery.getQuestion() != null) {
            example.and().andEqualTo("question", salaryFeedbackQuery.getQuestion());
        }
        if (salaryFeedbackQuery.getAnswer() != null) {
            example.and().andEqualTo("answer", salaryFeedbackQuery.getAnswer());
        }
        if (salaryFeedbackQuery.getAnswerTimeStart() != null &&
                salaryFeedbackQuery.getAnswerTimeEnd() != null) {
            example.and().andBetween("answerTime", salaryFeedbackQuery.getAnswerTimeStart(),
                    salaryFeedbackQuery.getAnswerTimeEnd());
        }
        if (salaryFeedbackQuery.getAnswerStatus() != null) {
            example.and().andEqualTo("answerStatus", salaryFeedbackQuery.getAnswerStatus());
        }
        if (salaryFeedbackQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", salaryFeedbackQuery.getCompanyId());
        }
        // 排序
        ExampleKit.setExampleOrder(example,salaryFeedbackQuery.getOrders());
        return example;
    }
}




